package com.cn.qgx428.orderingmeal.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.security.keystore.KeyInfo;
import android.support.annotation.NonNull;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;
import android.widget.Toast;

import com.cn.qgx428.orderingmeal.Mine;
import com.cn.qgx428.orderingmeal.R;
import com.cn.qgx428.orderingmeal.adapter.PayCardOptionsAdapter;
import com.cn.qgx428.orderingmeal.myClass.Bankcard;
import com.cn.qgx428.orderingmeal.myClass.MyKeyInfo;
import com.cn.qgx428.orderingmeal.myClass.User;
import com.jungly.gridpasswordview.GridPasswordView;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobPointer;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * Created by QinX on 2017/5/13.
 */

public class PayDialog extends Dialog implements View.OnClickListener {

    private static final String TAG = "PayDialog";

    private Context context;

    private TextView sureBtn;
    private TextView cancelBtn;
    private TextView title;

    private String userPayPassword;

    private String userID;

    private GridPasswordView passwordView;

    private TextView payWay;
    private RecyclerView recyclerView;
    private PayCardOptionsAdapter adapter;
    private List<String> cardNOs;

    /**
     * 标记要是否选择了银行卡
     */
    public static final int CARD_BE_CHOOSED = 0;
    public static final int CARD_NOT_BE_CHOOSED = 1;
    public int CARD_STATE = CARD_NOT_BE_CHOOSED;

    /**
     * 切换银行卡选项的显示和隐藏
     */
    private static final int SHOW_R = 0;
    private static final int HIDE_R = 1;
    private int R_STATE = HIDE_R;

    /**
     * 支付先确定有银行卡
     */
    public static final int HAS_CARD = 0;
    public static final int NOT_CARD = 1;
    public int USER_CARD = HAS_CARD;

    public PayDialog(@NonNull Context context, String userPayPassword, String userID) {
        super(context);
        this.context = context;
        this.userPayPassword = userPayPassword;
        this.userID = userID;
        setPayDialog();
    }

    private void setPayDialog() {
        View view = LayoutInflater.from(context).inflate(R.layout.pay_dialog, null);
        passwordView = (GridPasswordView) view.findViewById(R.id.pay_password);
        sureBtn = (TextView) view.findViewById(R.id.pay_sure);
        cancelBtn = (TextView) view.findViewById(R.id.pay_cancel);
        cancelBtn.setOnClickListener(this);
        title = (TextView) view.findViewById(R.id.pay_title);
        if ("".equals(userPayPassword) || userPayPassword == null) {
            title.setText("请设置支付密码");
            sureBtn.setText("设置");
        }
        payWay = (TextView) view.findViewById(R.id.pay_payWay);
        recyclerView = (RecyclerView) view.findViewById(R.id.pay_cardOptionsRecycler);
        payWay.setOnClickListener(this);
        getCardData();
        setContentView(view);
    }


    private String changeCardNoFomat(String cardNO) {
        String str = cardNO.replace(" ", "");
        str = "**** **** " + str.substring(str.length() - 4, str.length());
        return str;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.pay_cancel:
                dismiss();
                break;
            case R.id.pay_payWay:
                changeCardBeChoosed(CARD_STATE);
        }
    }

    private void getCardData() {
        User user = new User();
        user.setObjectId(userID);
        BmobQuery<Bankcard> query = new BmobQuery<Bankcard>();
        query.addWhereEqualTo("user", new BmobPointer(user));
        query.addQueryKeys("cardID");
        query.findObjects(new FindListener<Bankcard>() {
            @Override
            public void done(List<Bankcard> list, BmobException e) {
                if (e == null) {
                    //无银行卡先添加银行卡
                    if (list.size() == 0) {
                        payWay.setText("请先添加银行卡");
                        USER_CARD = NOT_CARD;
                    } else {
                        USER_CARD = HAS_CARD;
                        payWay.setText("付款方式");
                        adapter = new PayCardOptionsAdapter(context, list, R.layout.pay_dialog_card_item);
                        adapter.setCardOnclickListener(new PayCardOptionsAdapter.onCardNOOclickListener() {
                            @Override
                            public void onClick(String cardNO) {
                                payWay.setText(cardNO);
                                changeCardOptiontLayout(R_STATE);
                                CARD_STATE  = CARD_BE_CHOOSED;
                            }
                        });
                        recyclerView.setLayoutManager(new LinearLayoutManager(context));
                        recyclerView.setItemAnimator(new DefaultItemAnimator());
                        recyclerView.setAdapter(adapter);
                    }
                }

            }
        });
    }

    private void changeCardBeChoosed(int state) {
        if (USER_CARD == NOT_CARD){
            Intent intent = new Intent(context, Mine.class);
            MyKeyInfo keyInfo = new MyKeyInfo();
            keyInfo.setUserID(userID);
            intent.putExtra("keyInfo",keyInfo);
            context.startActivity(intent);
            dismiss();
        } else if (USER_CARD == HAS_CARD){
            //暂无银行卡被选择，显示选择项
            if (state == CARD_NOT_BE_CHOOSED) {
                getCardData();
            }
            changeCardOptiontLayout(R_STATE);
        }

    }

    private void changeCardOptiontLayout(int r_state) {
        if (r_state == SHOW_R) {
            recyclerView.setVisibility(View.GONE);
            R_STATE = HIDE_R;
        } else if (r_state == HIDE_R) {
            recyclerView.setVisibility(View.VISIBLE);
            R_STATE = SHOW_R;
        }
    }


    public void setSureClickListener(View.OnClickListener listener) {
        sureBtn.setOnClickListener(listener);
    }

    public String getPassword() {
        return passwordView.getPassWord();
    }

    public Boolean isChooseCard(){
        return CARD_STATE == CARD_BE_CHOOSED ? true : false;
    }

}
